package br.ufpe.cin.log.generator.impl;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import br.ufpe.cin.log.generator.service.LogGenerator;

public class LogGeneratorImpl implements LogGenerator {
	
	private Level level = null;
	private Logger logger = null;
	FileHandler fileHandlerResponseTime = null;
	FileHandler fileHandlerAvailability = null;
	FileHandler fileHandlerRequest = null;
	FileHandler fileHandlerError = null;
	FileHandler fileHandlerViolacao = null;
	
	public LogGeneratorImpl(){
		if (level == null)
			level = Level.ALL;
		
		logger = Logger.getLogger("LogGenerator");
		try {
			//fh = new FileHandler(nomeArquivo,true); com isso ele escreve no mesmo arquivo
			
			//cria outro arquivo
			
			MyFormatter formatter = new MyFormatter();
			
			fileHandlerResponseTime = new FileHandler("logResponseTime.txt");
			MyFilter filterResponseTime = new MyFilter("ResponseTime");
			fileHandlerResponseTime.setFilter(filterResponseTime);
			fileHandlerResponseTime.setFormatter(formatter);
			
			fileHandlerAvailability = new FileHandler("logAvailability.txt");
			MyFilter filterAvailability = new MyFilter("Availability");
			fileHandlerAvailability.setFilter(filterAvailability);
			fileHandlerAvailability.setFormatter(formatter);
			
			fileHandlerRequest = new FileHandler("logRequest.txt");
			MyFilter filterRequest = new MyFilter("Request");
			fileHandlerRequest.setFilter(filterRequest);
			fileHandlerRequest.setFormatter(formatter);
			
			fileHandlerError = new FileHandler("logError.txt");
			MyFilter filterError = new MyFilter("Error");
			fileHandlerError.setFilter(filterError);
			fileHandlerError.setFormatter(formatter);
			
			fileHandlerViolacao = new FileHandler("logViolacao.txt");
			MyFilter filterViolacao = new MyFilter("Violacao");
			fileHandlerViolacao.setFilter(filterViolacao);
			fileHandlerViolacao.setFormatter(formatter);
			
			
			logger.setUseParentHandlers(false);
			logger.addHandler(fileHandlerResponseTime);
			logger.addHandler(fileHandlerAvailability);
			logger.addHandler(fileHandlerRequest);
			logger.addHandler(fileHandlerError);
			logger.addHandler(fileHandlerViolacao);
			
			logger.setLevel(level);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void setLevel(Level level){
		this.logger.setLevel(level);
	}

	public void log(Level level, String mensagem){
		this.logger.log(level, mensagem);
	}

}
